home *** CD-ROM | disk | FTP | other *** search
- /*
- * Failure.h
- * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
- * Simple Failure handler.
- */
-
- #ifndef _H_Failure
- #define _H_Failure
- #include <setjmp.h>
- #include <Types.h>
-
- /*
- * The FailureRecord contains the information we need to
- * manage and, possibly, recover from errors.
- */
- typedef struct FailureRecord {
- jmp_buf jmpBuf;
- OSErr status;
- short messageIndex;
- } FailureRecord, *FailureRecordPtr;
-
- /*
- * This is a slightly simplified implementation of the Think/MPW/whatever
- * "Failure" mechanism. To use, define error handlers using the following macros:
- * TRY { initialize a failure unit.
- * } CATCH { end of the "normal" sequence, begin error handler.
- * } ENDTRY; end of the error unit.
- * NO_PROPAGATE (Inside the CATCH segment: do not pass the error
- * to the higher-level).
- * Failure(status, message) Initiate error recovery.
- * FailNIL(ptr/handle) Error if thing is NULL
- * FailOSErr(status, message) Error if status != noErr
- * SET_MESSAGE(message) Sets the message index if non currently specified.
- * If an error is not caught (in a sequence with NO_PROPAGATE), it will eventually
- * force an error alert with the indicated message.
- */
- #define FATAL (TRUE)
- #define NON_FATAL (FALSE)
- #define TRY { \
- FailureRecordPtr __oldFailurePtr; \
- FailureRecord __failureRecord; \
- __oldFailurePtr = gFailurePtr; \
- gFailurePtr = &__failureRecord; \
- if ((__failureRecord.status \
- = setjmp(__failureRecord.jmpBuf)) == noErr) {
- /* Normal code here */
-
- #define CATCH \
- } else {
- /* Error handler here */
- #define ENDTRY \
- } \
- gFailurePtr = __oldFailurePtr; \
- if (__failureRecord.status != noErr) { \
- FailureNeverDebug( \
- __failureRecord.status, \
- __failureRecord.messageIndex \
- ); \
- } \
- }
-
- #define STATUS (gFailurePtr->status)
- #define MESSAGE (gFailurePtr->messageIndex)
- #define NO_PROPAGATE do { \
- gFailurePtr->status = noErr; \
- } while (0)
-
- #ifdef THINK_C
- #define Failure(status, message) do { \
- gFailureLine = __LINE__; \
- gFailureFile = "\p" __FILE__; \
- __Failure(status, message); \
- } while (0)
- #define FailOSErr(status, message) do { \
- OSErr _status = (status); \
- if (_status != noErr) { \
- Failure(_status, message); \
- } \
- } while (0)
- #define FailOSErrNeverDebug(status, message) do { \
- OSErr _status = (status); \
- if (_status != noErr) { \
- gFailureLine = __LINE__; \
- gFailureFile = "\p" __FILE__; \
- FailureNeverDebug(_status, message); \
- } \
- } while (0)
- #define FailNIL(what, message) do { \
- if ((what) == NULL) { \
- gFailureLine = __LINE__; \
- gFailureFile = "\p" __FILE__; \
- __FailNIL(message); \
- } \
- } while (0)
- #else
- /*
- * MPW can't handle __FILE__ in macros.
- */
- #define Failure(status, message) do { \
- gFailureLine = __LINE__; \
- gFailureFile = "\p"; \
- __Failure(status, message); \
- } while (0)
- #define FailOSErr(status, message) do { \
- OSErr _status = (status); \
- if (_status != noErr) { \
- Failure(_status, message); \
- } \
- } while (0)
- #define FailOSErrNeverDebug(status, message) do { \
- OSErr _status = (status); \
- if (_status != noErr) { \
- gFailureLine = __LINE__; \
- gFailureFile = "\p"; \
- FailureNeverDebug(_status, message); \
- } \
- } while (0)
- #define FailNIL(what, message) do { \
- if ((what) == NULL) { \
- gFailureLine = __LINE__; \
- gFailureFile = "\p"; \
- __FailNIL(message); \
- } \
- } while (0)
- #endif
-
- void __Failure(
- OSErr errorStatus,
- short messageIndex
- );
- void FailureNeverDebug(
- OSErr errorStatus,
- short messageIndex
- );
- void __FailNIL(
- short messageIndex
- );
-
- extern StringPtr gFailureFile;
- extern long gFailureLine;
- extern FailureRecordPtr gFailurePtr;
- extern Boolean gDebugOnError;
-
- #endif
-